using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using TreeView.Logic;
using System.Drawing.Drawing2D;
using WindowsApplication2.TreeView.Graphic;

namespace TreeView.Graphic
{
    public class RadNodeGUI
    {
        #region Members

        /// <summary>
        /// The logic part of this node
        /// </summary>
        RadialTreeNode logicNode;
        /// <summary>
        /// The color of the node when we paint the tree
        /// </summary>
        private Brush nodeBrush;
        /// <summary>
        /// Uses as defaulr color for the node body after we mark it, 
        /// (i.e. if we move over it with the mouse).
        /// </summary>
        private Brush defaultBrush;
        /// <summary>
        /// Brush for the lines which conect nodes
        /// </summary>
        LinearGradientBrush LinearBrush;
        /// <summary>
        /// Color of the line which conect nodes
        /// </summary>
        private Color brunchColor;
        /// <summary>
        /// pen to draw the line form one node to another
        /// </summary>
        private Pen pn;
        /// <summary>
        /// size of the node in pixels
        /// </summary>
        private int nodeSize;
        /// <summary>
        /// pointer to the left child on the tree
        /// </summary>
        RadNodeGUI leftChild;
        /// <summary>
        /// pointer to the right child on the tree
        /// </summary>
        RadNodeGUI rightChild;
        /// <summary>
        /// Rectangle in which the node will be drawn
        /// </summary>
        Rectangle nodeBody;
        /// <summary>
        /// Position of the node
        /// </summary>
        PointF position;
        /// <summary>
        /// The name of the genome
        /// </summary>
        private string genomeName;

        #endregion

        #region Constructors

        public RadNodeGUI(RadialTreeNode node,int newNodeSize,Color lineColor,Brush brushForUse,Brush NewDefaultBrush,string genName,int winWidth,int winHeight)
        {
            logicNode = node;
            nodeSize = newNodeSize;
            brunchColor = lineColor;
            nodeBrush = brushForUse;
            defaultBrush = NewDefaultBrush;
            genomeName = genName;
            pn = new Pen(brunchColor, (float)1);
            position = new PointF((float)(winWidth / 2 + (winWidth / 2) * 0.98 * node.X), (float)(winHeight / 2 + (winHeight / 2) * 0.98 * node.Y));
        }
        
        #endregion

        #region Properties

        public Rectangle NodeBody
        {
            get { return nodeBody; }
        }

        public RadialTreeNode LogicNode
        {
            get { return logicNode; }
        }

        public RadNodeGUI LeftChild
        {
            set { leftChild = value; }
            get { return leftChild; }
        }

        public RadNodeGUI RightChild
        {
            set { rightChild = value; }
            get { return rightChild; }
        }

        public Brush NodeBrush
        {
            set { nodeBrush = value; }
            get { return nodeBrush; }
        }

        public Color BrunchColor
        {
            get { return brunchColor; }
            set { brunchColor = value; }
        }
        
        #endregion

        #region Methods

        internal void resetBrush()
        {
            nodeBrush = defaultBrush;
        }

        


        public void paintBody(Panel board)
        {
            using (Graphics g = board.CreateGraphics())
            {
                g.FillEllipse(nodeBrush, nodeBody);
            }
        }

        public void paintNode(Panel board)
        {
            using (Graphics g = board.CreateGraphics())
            {
                if ((leftChild != null) && (rightChild != null))
                {
                    LinearBrush = new LinearGradientBrush(position, leftChild.position, brunchColor, leftChild.brunchColor);
                    //print a branch to leftSon
                    pn = new Pen(LinearBrush, (float)2);
                    g.DrawLine(pn, position, leftChild.position);
                    //print a branch to rightSon
                    LinearBrush = new LinearGradientBrush(position, rightChild.position, brunchColor, rightChild.brunchColor);
                    pn = new Pen(LinearBrush, (float)2);
                    g.DrawLine(pn, position, rightChild.position);
                }
                //create the node body(give the node some shape)
                nodeBody = new Rectangle((int)position.X - nodeSize / 2, (int)position.Y - nodeSize / 2, nodeSize, nodeSize);
            }
        }


        public void paintBody(Graphics g)
        {
            //using (Graphics g = e.Graphics)
            {
                g.FillEllipse(nodeBrush, nodeBody);
            }
        }

        public void paintNode(Graphics g)
        {
            //using (Graphics g = e.Graphics)
            {
                if ((leftChild != null) && (rightChild != null))
                {
                    LinearBrush = new LinearGradientBrush(position, leftChild.position, brunchColor, leftChild.brunchColor);
                    //print a branch to leftSon
                    pn = new Pen(LinearBrush, 1);
                    g.DrawLine(pn, position, leftChild.position);
                    //print a branch to rightSon
                    LinearBrush = new LinearGradientBrush(position, rightChild.position, brunchColor, rightChild.brunchColor);
                    pn = new Pen(LinearBrush, (float)1);
                    g.DrawLine(pn, position, rightChild.position);
                }
                //create the node body(give the node some shape)
                nodeBody = new Rectangle((int)position.X - nodeSize / 2, (int)position.Y - nodeSize / 2, nodeSize, nodeSize);
            }
        }

        //public void paintBody(Panel board)
        //{
        //    using (Graphics g = board.CreateGraphics())
        //    {
        //        g.FillEllipse(nodeBrush, nodeBody);
        //    }
        //}

        #endregion
    }
}
